'''
导入包：import csv
csv.reader()
csv.writer()
'''
import csv

# 案例1：读取
with open(r'username.csv') as f:
    f_csv_reader = csv.reader(f)
    for data in f_csv_reader:
        print(data)

# 看个案例：enumerate()
list1 = list(range(1, 11, 2))
a = enumerate(list1)
print(a)
b = list(a)
print(b)

# 忽略首行，怎么办？？？方式1
with open(r'username.csv') as f:
    f_csv_reader = csv.reader(f)
    for i, data in enumerate(f_csv_reader):
        if i == 0:
            continue
        else:
            print(data)

# 忽略首行，怎么办？？？方式2
with open(r'username.csv') as f:
    f_csv_reader = csv.reader(f)
    next(f_csv_reader)
    for data in f_csv_reader:
        print(data)

# 案例2：写入
user_feild = ['username', 'password']
user1 = ['test01', '123456']
user2 = ['test02', '123456']
user3 = ['test03', '123456']
user_list = [user_feild, user1, user2, user3]

with open(r'username2.csv', mode='w', newline='') as f:
    f_csv_writer = csv.writer(f)
    f_csv_writer.writerows(user_list)

'''
综合案例：
定义函数  get_csv_data()、get_data_from_csv()
定义类    Csv_Util()
参数：
返回值：列表嵌套列表，不要首行
[['test01', '123456'], ['test02', '123456'], ['test03', '123456']]
'''
def get_csv_data(path):
    data_list = []
    with open(path,mode='r',encoding='utf-8') as f:
        f_csv_reader = csv.reader(f)
        next(f_csv_reader)
        for data in f_csv_reader:
            data_list.append(data)
    return data_list

if __name__ == '__main__':
    path = r'username.csv'
    print(get_csv_data(path))
